package com.hanter.android.radui.mvp;

import android.content.Context;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.hanter.android.radlib.util.WindowUtils;
import com.hanter.android.radui.R;
import com.hanter.android.radwidget.statusbar.StatusBarHelper;
import com.hanter.android.radwidget.WindowInsetsFrameLayout;

public abstract class MvpTitleBarActivity<V extends BaseView, T extends BasePresenter<V>>
        extends BaseActivity implements BaseView, View.OnClickListener {

    protected TitleBarHelper titleBarHelper;
    protected WindowInsetsFrameLayout contentContainer;
    protected View contentView;
    protected T presenter;
    private View customTitleView; // 自定义标题栏
    private ContentLoadStatusViewHelper contentStatusViewHelper;
    private LoadingDialogHelper loadingDialogHelper;
    private boolean showLoading;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rad_mvp);

        WindowUtils.setTranslucentStatus(this, true);
        StatusBarHelper.setStatusBarMode(this, true);

        initBaseContentView();
        initBaseViews();
        initTitleBar();

        presenter = createPresenter();
        //noinspection unchecked
        presenter.attachView((V) this);

        loadingDialogHelper = new LoadingDialogHelper(getSupportFragmentManager());
    }

    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        super.onTitleChanged(title, color);
        this.titleBarHelper.title.setText(title);
    }

    private void initBaseViews() {
        titleBarHelper = new TitleBarHelper(this);
        titleBarHelper.leftBar.setOnClickListener(this);
        titleBarHelper.leftBarButton.setOnClickListener(this);
        titleBarHelper.rightBar.setOnClickListener(this);
        titleBarHelper.rightBarButton.setOnClickListener(this);
    }

    protected void initTitleBar() {
        titleBarHelper.initTitleBar();
    }

    /**
     * 加载框、无数据、主界面三种状态的处理
     */
    protected void initBaseContentView() {
        contentContainer = findViewById(R.id.base_content_container);
        createAddContentView();
        contentStatusViewHelper = new ContentLoadStatusViewHelper(MvpTitleBarActivity.this, contentContainer, contentView);
        contentStatusViewHelper.initViews(showLoading);
    }

    /**
     * 主界面
     */
    private void createAddContentView() {
        contentView = inflate(getLayout(), contentContainer);

        if (contentView != null) {
            contentContainer.addView(contentView);
            if (showLoading) {
                contentView.setVisibility(View.INVISIBLE);
            }
        }
    }

    @LayoutRes
    protected abstract int getLayout();

    public View inflate(@LayoutRes int resId, ViewGroup parent) {
        return LayoutInflater.from(this).inflate(resId, parent, false);
    }

    /**
     * 必须在onCreate之前调用（否则不会显现加载框）
     *
     * @param showLoading 是否显示加载
     */
    public void setShowContentLoading(boolean showLoading) {
        this.showLoading = showLoading;
    }

    @Override
    public Context getContext() {
        return this;
    }

    public abstract T createPresenter();

    @Override
    public void showProgressDialog() {
        loadingDialogHelper.showProgressDialog();
    }

    @Override
    public void disProgressDialog() {
        loadingDialogHelper.dismissProgressDialog();
    }

    @Override
    public void hideContentView() {
        contentStatusViewHelper.hideContentView();
    }

    @Override
    public void showContentView() {
        contentStatusViewHelper.showContentView();
    }

    @Override
    public void showNoData() {
        contentStatusViewHelper.showNoDataView();
    }

    @Override
    public void hideNoData() {
        contentStatusViewHelper.hideExceptionView();
    }

    @Override
    public void showPageFault() {
        contentStatusViewHelper.showPageFaultView();
    }

    @Override
    public void showNoNetWork() {
        contentStatusViewHelper.showNoNetworkView();
    }

    @Override
    public void showLoadingView() {
        contentStatusViewHelper.showLoadingView();
    }

    @Override
    public void completeLoading() {
        contentStatusViewHelper.completeLoading();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.leftBarButton) {
            onBackPressed();
        } else if (id == R.id.btnContentExceptionRefresh) {
            showLoadingView();
            onContentRefreshClick();
        }
    }

    @Override
    public void navBack() {
        onBackPressed();
    }

    protected void onContentRefreshClick() {

    }

    public void setColorPrimaryDark(@ColorInt int color) {
        titleBarHelper.compatPrimaryDark.setBackgroundColor(color);
    }

    public void setTitleBackground(@DrawableRes int resId) {
        titleBarHelper.titleBar.setBackgroundResource(resId);
    }

    public void setColorPrimary(@ColorInt int color) {
        titleBarHelper.titleBar.setBackgroundColor(color);
    }

    public void setCustomTitleView(@LayoutRes int resId) {
        View titleView = LayoutInflater.from(getContext()).inflate(resId, titleBarHelper.realTitleBar, false);
        setCustomTitleView(titleView);
    }

    public void setCustomTitleView(@NonNull View titleView) {
        if (customTitleView != null) {
            titleBarHelper.realTitleBar.removeView(customTitleView);
        }
        ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_CONSTRAINT,
                ConstraintLayout.LayoutParams.MATCH_CONSTRAINT);
        params.topToTop = R.id.titleBar;
        params.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;
        params.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID;
        params.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
        titleBarHelper.realTitleBar.addView(titleView, params);
        customTitleView = titleView;
    }

    /**
     * 设置标题栏可见
     *
     * @param visibility visibility
     */
    public void setTitleBarVisibility(int visibility) {
        titleBarHelper.titleBar.setVisibility(visibility);
        titleBarHelper.titleBarLine.setVisibility(visibility);
        initTitleBar();
    }

    @SuppressWarnings("unused")
    public void setLeftTitleText(@NonNull String title) {
        titleBarHelper.leftBarButton.setText(title);
    }

    @SuppressWarnings("unused")
    public void setLeftTitleText(@StringRes int resId) {
        titleBarHelper.leftBarButton.setText(resId);
    }

    public void setRightTitleText(@NonNull String title) {
        titleBarHelper.rightBarButton.setText(title);
    }

    public void setRightTitleTextColor(@ColorInt int color) {
        titleBarHelper.rightBarButton.setTextColor(color);
    }

    @SuppressWarnings("unused")
    public void setRightTitleText(@StringRes int resId) {
        titleBarHelper.rightBarButton.setText(resId);
    }

    @SuppressWarnings("unused")
    public void setRightTextImage(@NonNull String title, @DrawableRes int image) {
        titleBarHelper.rightBarButton.setText(title);
        titleBarHelper.rightBarButton.setCompoundDrawablesWithIntrinsicBounds(image, 0, 0, 0);
        int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getContext().getResources().getDisplayMetrics());
        titleBarHelper.rightBarButton.setCompoundDrawablePadding(padding);
    }

    @SuppressWarnings("unused")
    public void setLeftTitleImage(@DrawableRes int resId) {
        titleBarHelper.leftBarButton.setCompoundDrawablesWithIntrinsicBounds(resId, 0, 0, 0);
    }

    @SuppressWarnings("unused")
    public void setRightTitleImage(@DrawableRes int resId) {
        titleBarHelper.rightBarButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, resId, 0);
    }

    public ImageView addRightImage(@DrawableRes int src, int dpL, int dpR) {
        ImageView button = new ImageView(getContext());
        button.setImageResource(src);
        titleBarHelper.rightBar.addView(button);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        params.weight = ViewGroup.LayoutParams.WRAP_CONTENT;
        button.setPadding(dpL, 0, dpR, 0);
        return button;
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        if (presenter != null)
            presenter.detachView();
        disProgressDialog();
        super.onDestroy();
    }
}
